import 'package:flutter/material.dart';
import 'package:get/get.dart';

///单选弹窗
class DnpSingleChoiceDialog extends StatefulWidget {
  final ValueChanged<String>? onConfirm;
  final VoidCallback? onCancel;
  final List<String> options;
  final String? selectedOption;

  final String title;

  const DnpSingleChoiceDialog({
    super.key,
    required this.options,
    required this.title,
    this.onConfirm,
    this.onCancel,
    this.selectedOption,
  });

  @override
  State<DnpSingleChoiceDialog> createState() => _DnpSingleChoiceDialogState();
}

class _DnpSingleChoiceDialogState extends State<DnpSingleChoiceDialog> {
  int selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    selectedIndex = widget.options
        .indexOf(widget.selectedOption ?? "")
        .clamp(0, widget.options.length - 1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Center(
            child: Container(
                width: 308.45,
                height: 393.6,
                padding: EdgeInsets.symmetric(horizontal: 14),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(
                            'assets/images/ic_identity_single_choice_bg.webp'),
                        fit: BoxFit.fill)),
                child: Column(children: [
                  SizedBox(height: 66),
                  Text(
                    widget.title,
                    style: TextStyle(
                        fontWeight: FontWeight.w600,
                        fontSize: 16,
                        color: Color(0xFF252630)),
                  ),
                  SizedBox(height: 14),
                  Container(
                    width: 267,
                    height: 210,
                    margin: EdgeInsets.only(right: 13),
                    child: Align(
                      alignment: Alignment.center,
                      child: ListView.builder(
                          itemCount: widget.options.length,
                          padding: EdgeInsets.zero,
                          itemBuilder: (context, index) {
                            return GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              onTap: () {
                                setState(() {
                                  selectedIndex = index;
                                });
                              },
                              child: Container(
                                height: 40,
                                width: double.infinity,
                                alignment: Alignment.center,
                                decoration: selectedIndex == index
                                    ? BoxDecoration(
                                        borderRadius: BorderRadius.circular(20),
                                        color: Color(0xFF1D4AC8))
                                    : null,
                                child: Text(
                                  widget.options[index],
                                  style: TextStyle(
                                      fontWeight: FontWeight.w400,
                                      fontSize: 14,
                                      color: selectedIndex == index
                                          ? Colors.white
                                          : Color(0xFF333333)),
                                ),
                              ),
                            );
                          }),
                    ),
                  ),
                  SizedBox(height: 14),
                  _buildBottomButton()
                ]))));
  }

  Widget _buildBottomButton() {
    return Row(
      children: [
        Expanded(
            child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            widget.onCancel?.call();
            Get.back();
          },
          child: Container(
            width: double.infinity,
            height: 48,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color(0xFFCFE6FF),
              borderRadius: BorderRadius.circular(16),
            ),
            child: Text(
              "Back",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: Color(0xFF95A5B9),
              ),
            ),
          ),
        )),
        const SizedBox(
          width: 9,
        ),
        Expanded(
            child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            widget.onConfirm?.call(widget.options[selectedIndex]);
            Get.back();
          },
          child: Container(
            width: double.infinity,
            height: 48,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color(0xFF1D4AC8),
              borderRadius: BorderRadius.circular(16),
            ),
            child: Text(
              "Confirm",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: Colors.white,
              ),
            ),
          ),
        )),
        const SizedBox(
          width: 13,
        ),
      ],
    );
  }
}
